package com.asura.app.wechat.common;

import android.content.Context;

import com.asura.app.library.ModuleProvider;
import com.asura.app.library.imageload.GlideImageLoader;
import com.asura.app.library.imageload.ImageLoader;
import com.asura.app.library.initializer.Initializer;
import com.asura.app.wechat.Config;
import com.asura.app.wechat.repository.MomentDataRepository;
import com.asura.app.wechat.repository.UserInfoDataRepository;
import com.asura.app.wechat.repository.datasource.MomentDataSource;
import com.asura.app.wechat.repository.datasource.UserInfoDataSource;
import com.asura.app.wechat.repository.net.WechatApi;
import com.asura.app.wechat.repository.net.interceptor.LoggingInterceptor;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Asura on 2017/12/27.
 */

public class WechatModuleProvider implements ModuleProvider, Initializer {
    public static final WechatModuleProvider sInstance = new WechatModuleProvider();
    private OkHttpClient okHttpClient;
    private Retrofit retrofit;
    private Gson gson;
    private WechatApi wechatApi;
    private ImageLoader imageLoader;
    private MomentDataSource momentDataSource;
    private UserInfoDataSource userInfoDataSource;

    public static WechatModuleProvider getInstance() {
        return sInstance;
    }


    @Override
    public void initialize(Context context, boolean debuggable) throws Exception {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS);
        if (debuggable) {
            builder.addInterceptor(new LoggingInterceptor());
            builder.addNetworkInterceptor(new StethoInterceptor());
        }
        okHttpClient = builder.build();
        gson = new GsonBuilder()
                .setLenient()
                .setDateFormat(Config.DATE_FORMAT)
                .create();
        retrofit = new Retrofit.Builder().baseUrl(Config.API_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        wechatApi = retrofit.create(WechatApi.class);
        imageLoader = new GlideImageLoader(context);
        momentDataSource = new MomentDataRepository();
        userInfoDataSource = new UserInfoDataRepository();
    }

    @Override
    public OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    public Gson getGson() {
        return gson;
    }

    public WechatApi getWechatApi() {
        return wechatApi;
    }

    public ImageLoader getImageLoader() {
        return imageLoader;
    }

    public MomentDataSource getMomentDataSource() {
        return momentDataSource;
    }

    public UserInfoDataSource getUserInfoDataSource() {
        return userInfoDataSource;
    }
}
